﻿using OSSUtility;
using Ozone.Help;
using Ozone.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.DAL
{
    public class RobotPointsDAL
    {
        //查询
        public static List<robot_points> QueryRobotPoint(string sPoName = "",DateTime Starttime = default, DateTime EndTime = default)
        {
            List<robot_points> robot_p = new List<robot_points>();
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (!string.IsNullOrEmpty(sPoName))
                {
                    //PoName查询
                    robot_p = db.Queryable<robot_points>()
                    .Where(p => p.PoName == sPoName)
                    .OrderBy(p => p.CreateTime, OrderByType.Desc)
                    .ToList();
                }
                else if( EndTime > Starttime)
                {
                    //根据时间查询
                    robot_p = db.Queryable<robot_points>()
                    .Where(p => p.CreateTime >= Starttime && p.CreateTime <= EndTime)
                    .OrderBy(p => p.CreateTime, OrderByType.Desc)
                    .ToList();
                }
                LogHelper.Debug("查询机械臂点位完成");
                return robot_p;
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询机械臂点位异常",ex);
                return robot_p;
            }
        }
        //添加
        public static bool AddRobotPoint(List<robot_points> robot_p)
        {
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (robot_p.Count>0)
                {
                    foreach (robot_points p in robot_p)
                    {
                        p.FID = Guid.NewGuid().ToString();
                        p.CreateTime = DateTime.Now;
                        p.ModifyTime = DateTime.Now;
                    }
                    if (db.Insertable<robot_points>(robot_p).ExecuteCommand() > 0)
                    {
                        LogHelper.Debug("添加机械臂点位完成");
                        return true;
                    }
                    else
                    {
                        LogHelper.Debug("添加机械臂点位失败");
                        return false;
                    }
                }
                LogHelper.Debug("添加机械臂点位异常,robot_p为null");

                return false;
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加机械臂点位异常",ex);
                return false;
            }
        }
        //删除
        public static bool DeleteRobotPoint(List<string> lstPoName)
        {
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                //单条或多条
                foreach(string poName in lstPoName)
                {
                    if(db.Deleteable<robot_points>().Where(p => p.PoName == poName).ExecuteCommand()>0)
                        LogHelper.Debug($"删除机械臂{poName}点位成功");
                    else
                        LogHelper.Debug( $"删除机械臂{poName}点位失败");
                }
                LogHelper.Debug("删除机械臂点位完成");
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加机械臂点位异常",ex);
                return false;
            }
        }

        //更改
        public static bool UpdateRobotPoint(List<robot_points> lstrobot_p)
        {
            bool IsSuccess = false;
            try
            {
                //单条或多条
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                foreach (robot_points robot_p in lstrobot_p)
                {
                    if (robot_p != null)
                    {
                        if (IsSuccess = db.Updateable<robot_points>()
                        .SetColumns(p => p.PoValue == robot_p.PoValue)
                        .SetColumns(p => p.ToolName == robot_p.ToolName)
                        .SetColumns(p => p.ToolValue == robot_p.ToolValue)
                        .SetColumns(p => p.Remarks == robot_p.Remarks)
                        .SetColumns(p => p.ModifyTime == robot_p.ModifyTime)
                        .Where(p => p.PoName == robot_p.PoName)
                        .ExecuteCommand() > 0)
                        {
                            LogHelper.Debug($"更改机械臂{robot_p.PoName}点位成功");
                        }
                        else
                            LogHelper.Debug( $"更改机械臂{robot_p.PoName}点位失败");
                    }
                    else
                        LogHelper.Debug($"更改机械臂点位异常,robot_p为null");
                }
                LogHelper.Debug("更改机械臂点位完成");
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加机械臂点位异常", ex);
                return false;
            }
        }

        //删除表中所有数据(未使用)
        public static bool DeleteAllData()
        {
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                int nDelete = db.Deleteable<robot_points>().ExecuteCommand();
                if(nDelete > 0)
                {
                    Log.writeLog("log", "删除机械臂所有点位完成");
                    return true;
                }
                else
                {
                    Log.writeLog("log", "删除机械臂所有点位异常");
                    return false;
                }
            }
            catch(Exception ex)
            {
                Log.writeLog("log", "添加机械臂点位异常," + ex);
                return false;
            }
        }
        //删除表(未使用)
        public static bool DeleteTable(string tableName)
        {
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                //string tableName = "robot_points";
                if (db.DbMaintenance.IsAnyTable(tableName))
                {
                    // 存在则删除
                    db.DbMaintenance.DropTable(tableName);
                    Log.writeLog("log", $"表 {tableName} 已删除");
                    return true;
                }
                else
                {
                    Log.writeLog("log", $"表 {tableName} 不存在");
                    return false;
                }
            }
            catch (Exception ex)
            {
                Log.writeLog("log", $"删除表{tableName}异常,"+ex);
                return false;
            }
        }
    }
}